﻿
#include "CServer.h"
#include<iostream>
#include<csignal>
#include<thread>
#include<mutex>
#include"AsioIOServicePool.h"
using namespace std;
bool bstop = false;
std::condition_variable cond_quit;
std::mutex mutex_quit;
void sig_handler(int sig) {
	if (sig == SIGINT || sig == SIGTERM) {
		std::unique_lock<std::mutex> lock_quit(mutex_quit);
		bstop = true;
		cond_quit.notify_one();

	}
}
int main()
{
	try {
		auto pool = AsioIOServicePool::GetInstance();
		boost::asio::io_context  io_context;
		std::thread net_work_thread([&io_context]() {//用子线程跑服务器
			CServer s(io_context, 10086);
			io_context.run();
			});
		signal(SIGINT, sig_handler);//Ruo捕获到sigint信号则执行回调函数;
		signal(SIGTERM, sig_handler);

		while (!bstop) {
			std::unique_lock<std::mutex> lock_quit(mutex_quit);
			cond_quit.wait(lock_quit);

		}
		io_context.stop();
		net_work_thread.join();//让主线程挂起去等待子线程结束再结束
	}
	catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << endl;
	}
	boost::asio::io_context io_context;
}